package com.qb.login.ui.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.CompoundButton
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.blankj.utilcode.util.ToastUtils
import com.qb.baselibrary.basekotlin.EmptyViewModel
import com.qb.common.base.CommonBaseActivity
import com.qb.common.bean.LoginBean
import com.qb.common.bean.UserInfo
import com.qb.common.constant.Constants
import com.qb.common.router.RouterPath
import com.qb.common.utils.LitePalUtils
import com.qb.login.databinding.LoginActivityRegisterBinding
import kotlin.math.log

@Route(path = RouterPath.Login.URL_Login_RegisterActivity)
class RegisterActivity:CommonBaseActivity<EmptyViewModel,LoginActivityRegisterBinding>(), View.OnClickListener {
    var isCheck = false
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.btnRegister.setOnClickListener(this)
        mBinding.privacyAgreementTv.setOnClickListener(this)
        mBinding.llUserXy.setOnClickListener(this)
        mBinding.agreeCb.isChecked = false
        mBinding.agreeCb.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
            isCheck = isChecked
        })
    }

    override fun initData() {
    }

    override fun onClick(p0: View?) {
        p0?.let {
            when(p0.id){
                mBinding.privacyAgreementTv.id,mBinding.llUserXy.id->{
                    val packageName = AppUtils.getAppPackageName()
                    val className = "com.qb.common.ui.activity.WebViewActivity"
                    // 创建一个 Intent
                    val intent = Intent().apply {
                        setClassName(packageName, className)
                    }
                    // 启动目标 Activity
                    startActivity(intent)
                }
                mBinding.btnRegister.id->{
                    if (TextUtils.isEmpty(mBinding.etAccount.text)){
                        ToastUtils.showShort("账户不能为空")
                        return
                    }
                    if (TextUtils.isEmpty(mBinding.etPassword.text)){
                        ToastUtils.showShort("密码不能为空")
                        return
                    }
                    if (TextUtils.isEmpty(mBinding.etPasswordAgain.text)){
                        ToastUtils.showShort("确认密码不能为空")
                        return
                    }
                    var password = mBinding.etPassword.text.toString()
                    var passwordAgain = mBinding.etPasswordAgain.text.toString()
                    if (password!=passwordAgain){
                        ToastUtils.showShort("两次输入密码不一致")
                        return
                    }
//                    var loginBean = LoginBean()
//                    loginBean.username = mBinding.etAccount.text.toString()
//                    loginBean.password = mBinding.etPassword.text.toString()
//                    SPStaticUtils.put(Constants.LOGININFO,GsonUtils.toJson(loginBean))
                    if (!isCheck){
                        ToastUtils.showShort("请仔细阅读并勾选隐私协议")
                        return
                    }
                    var userInfo = UserInfo()
                    userInfo.username = "${mBinding.etAccount.text}"
                    userInfo.password = "${mBinding.etPassword.text}"
                    LitePalUtils.saveUser(userInfo)
                    finish()
                }

                else -> {}
            }
        }
    }
}